package com.yuxi.Service.Impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.mapping.Array;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuxi.Config.ConfigStatic;
import com.yuxi.Dao.CommentRecordBrowseDao;
import com.yuxi.Dao.CommentRecordDao;
import com.yuxi.Dao.ConferenceRecordDao;
import com.yuxi.Dao.EvaluationRecordBrowseDao;
import com.yuxi.Dao.EvaluationRecordDao;
import com.yuxi.Dao.InformationClassificationDao;
import com.yuxi.Dao.InformationDao;
import com.yuxi.Dao.InformationOperationRecordDao;
import com.yuxi.Dao.InformationPortDao;
import com.yuxi.Dao.InformationPortPowerDao;
import com.yuxi.Dao.InformationQualityDao;
import com.yuxi.Dao.InformationSortDao;
import com.yuxi.Dao.InformationSourceDao;
import com.yuxi.Dao.ModifyInformationRecordDao;
import com.yuxi.Dao.PositionDao;
import com.yuxi.Dao.PowerModuleDao;
import com.yuxi.Dao.PowerRoleDao;
import com.yuxi.Dao.PrisonRoleDao;
import com.yuxi.Dao.PrisonStructureDao;
import com.yuxi.Dao.RoleDao;
import com.yuxi.Dao.TaskCompletionDao;
import com.yuxi.Dao.TaskDispatchDao;
import com.yuxi.Dao.UserInfoDao;
import com.yuxi.Dao.UserPositionDao;
import com.yuxi.Dao.UserStructureDao;
import com.yuxi.Dao.Impl.UserInfoDaoImpl;
import com.yuxi.Entity.CommentRecord;
import com.yuxi.Entity.CommentRecordBrowse;
import com.yuxi.Entity.ConferenceRecord;
import com.yuxi.Entity.EvaluationRecord;
import com.yuxi.Entity.EvaluationRecordBrowse;
import com.yuxi.Entity.Information;
import com.yuxi.Entity.InformationClassification;
import com.yuxi.Entity.InformationOperationRecord;
import com.yuxi.Entity.InformationPort;
import com.yuxi.Entity.InformationPortPower;
import com.yuxi.Entity.InformationQuality;
import com.yuxi.Entity.InformationSort;
import com.yuxi.Entity.InformationSource;
import com.yuxi.Entity.ModifyInformationRecord;
import com.yuxi.Entity.Position;
import com.yuxi.Entity.PowerModule;
import com.yuxi.Entity.PowerRole;
import com.yuxi.Entity.PrisonRole;
import com.yuxi.Entity.PrisonStructure;
import com.yuxi.Entity.Role;
import com.yuxi.Entity.TaskCompletion;
import com.yuxi.Entity.TaskDispatch;
import com.yuxi.Entity.UserInfo;
import com.yuxi.Entity.UserPosition;
import com.yuxi.Entity.UserView;
import com.yuxi.Request.Page;
import com.yuxi.Request.UserQuery;
import com.yuxi.Service.PrisonService;
import com.yuxi.Service.UserDoRecordService;
import com.yuxi.Service.UserService;
import com.yuxi.Service.UserSubService;
import com.yuxi.Util.StringManageUtil;
@Service
public class PrisonServiceImpl implements PrisonService{
	@Autowired
	private PrisonStructureDao prisonStructureDao;
	@Autowired
	private CommentRecordDao commentRecordDao;
	@Autowired
	private EvaluationRecordDao evaluationRecordDao;
	@Autowired
	private TaskDispatchDao taskDispatchDao;
	@Autowired
	private ModifyInformationRecordDao modifyInformationRecordDao;
	@Autowired
	private InformationSortDao informationSortDao;
	@Autowired
	private InformationClassificationDao informationClassificationDao;
	@Autowired
	private InformationSourceDao informationSourceDao;
	@Autowired
	private TaskCompletionDao taskCompletionDao;
	@Autowired
	private InformationQualityDao informationQualityDao;
	@Autowired
	private PowerModuleDao powerModuleDao;
	@Autowired
	private PrisonRoleDao prisonRoleDao;
	@Autowired
	private PowerRoleDao powerRoleDao;
	@Autowired
	private ConferenceRecordDao conferenceRecordDao;
	@Autowired
	private UserInfoDao userInfoDao ;
	@Autowired
	private InformationDao informationDao;
	@Autowired
	private UserDoRecordService userDoRecordService;
	@Autowired
	private InformationOperationRecordDao informationOperationRecordDao;
	@Autowired
	private UserSubService userSubService;
	@Autowired
	private UserService userService;
	@Autowired
	private CommentRecordBrowseDao commentRecordBrowseDao;
	@Autowired
	private InformationPortDao informationPortDao;
	@Autowired
	private InformationPortPowerDao informationPortPowerDao; 
	@Autowired 
	private PositionDao positionDao;
	@Autowired
	private UserPositionDao userPositionDao;
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private UserStructureDao userStructureDao;
	@Autowired
	private EvaluationRecordBrowseDao evaluationRecordBrowseDao;
	
	private Logger logger=LoggerFactory.getLogger(PrisonServiceImpl.class);
	@Override
	public PrisonStructure addStructure(PrisonStructure pStructure) {
		try {
			
			pStructure=prisonStructureDao.addStructure(pStructure);
			logger.info("添加监狱结构成功！");
			return pStructure;
		} catch (Exception e) {
			logger.error("添加监狱结构失败！！！");
			return null;
		}
		
	}
	@Override
	public PrisonStructure getStructure(Integer id) {
		try {
			
			PrisonStructure pStructure=prisonStructureDao.getStructure(id);
			logger.info("获取监狱结构成功！");
			return pStructure;
		} catch (Exception e) {
			logger.error("获取监狱结构失败！！！");
			return null;
		}
		
	}
	@Override
	public void modifyStructure(PrisonStructure pStructure) {
		try {
			String name=pStructure.getName();
			String remark=pStructure.getRemark();
			pStructure=prisonStructureDao.getStructure(pStructure.getId());
			pStructure.setName(name);
			pStructure.setRemark(remark);
			prisonStructureDao.modifly(pStructure);
			logger.info("修改监狱结构成功！");
		} catch (Exception e) {
			logger.info("修改监狱结构失败！！！");
		}
		
	}

	@Override
	public List<PrisonStructure> getAllStructure() {
		try {
			List<PrisonStructure> pStructures=	prisonStructureDao.getAllStructure();
			logger.info("获取所有监狱结构成功！");
			return pStructures;
		} catch (Exception e) {
			logger.error("获取所有监狱结构失败！！！");
			return null;
		}
		
	}

	@Override
	public List<PrisonStructure> getAllStructureBySuperId(Integer superId) {
		try {
			 List<PrisonStructure> pStructures=	prisonStructureDao.getAllStructureBySuperId(superId);
			 logger.info("通过上级id获取监狱结构成功！");
			 return pStructures;
		} catch (Exception e) {
			 logger.error("通过上级id获取监狱结构失败！！！");
			 return null;
		}
		
	}

	@Override
	public void deleteStructure(PrisonStructure pStructure) {
		try {
			prisonStructureDao.deleteStructure(pStructure);
			logger.info("删除监狱结构成功！");
		} catch (Exception e) {
			logger.error("删除监狱结构失败！！！");
		}
	   
	}

	@Override
	public InformationSort addSort(InformationSort iSort) {
		return informationSortDao.addISort(iSort);
	}

	@Override
	public List<InformationSort> getAllInformationSort() {
		return informationSortDao.getAllInformationSort();
	}

	@Override
	public List<InformationClassification> getAllInformationClassification() {
		// TODO Auto-generated method stub
		return informationClassificationDao.getAllInformationClassification();
	}

	@Override
	public List<InformationSource> getAllInformationSource() {
		// TODO Auto-generated method stub
		return  informationSourceDao.getAllInformationSource();
	}

	@Override
	public List<TaskCompletion> getAllTaskCompletion() {
		// TODO Auto-generated method stub
		return taskCompletionDao.getAllTaskCompletion();
	}

	@Override
	public List<InformationQuality> getAllInformationQuality() {
		// TODO Auto-generated method stub
		return informationQualityDao.getAllInformationQuality();
	}

	@Override
	public void deleteQuality(InformationQuality iQuality) {
		// TODO Auto-generated method stub
		informationQualityDao.deleteIQuality(iQuality);
	}

	@Override
	public void deleteCompletion(TaskCompletion tCompletion) {
		// TODO Auto-generated method stub
		taskCompletionDao.deletetCompletion(tCompletion);
	}

	@Override
	public void modifyQuality(InformationQuality iQuality) {
		// TODO Auto-generated method stub
		informationQualityDao.modifly(iQuality);
	}

	@Override
	public void modifyCompletion(TaskCompletion tCompletion) {
		// TODO Auto-generated method stub
		taskCompletionDao.modifly(tCompletion);
	}

	@Override
	public InformationQuality addQuality(InformationQuality iQuality) {
		return informationQualityDao.addIQuality(iQuality);
	}

	@Override
	public TaskCompletion addCompletion(TaskCompletion tCompletion) {
		return taskCompletionDao.addCompletion(tCompletion);
	}

	@Override
	public InformationSort addInfoSort(InformationSort iSort) {
		// TODO Auto-generated method stub
		return informationSortDao.addISort(iSort);
	}

	@Override
	public boolean addInfoSource(InformationSource iSource) {
		try {
			int count=informationSourceDao.getCountSource(iSource.getSuperId());
			iSource.setLevel((count+1));
			informationSourceDao.addInformationSource(iSource);
			logger.info("添加信息渠道成功");
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("添加信息渠道失败");
			return false;
		}
		
	}

	@Override
	public InformationClassification addInfoClassification(InformationClassification iClassification) {
		// TODO Auto-generated method stub
		return informationClassificationDao.addClassification(iClassification);
	}

	@Override
	public void modifyInfoSort(InformationSort iSort) {
		// TODO Auto-generated method stub
		informationSortDao.modifly(iSort);
	}

	@Override
	public boolean modifyInfoSource(Integer id,Integer level,Integer superId, String name,String remark,Integer lastSuperId) {
		try {
			List<InformationSource> sources = informationSourceDao.getAllInformationSource();
			int count=0;
			for (InformationSource informationSource : sources) {
				if (superId!=lastSuperId) {
					if (informationSource.getId()==superId) {
						count++;
					}
					if (informationSource.getId()==lastSuperId) {
						if (informationSource.getLevel()>level) {
							informationSource.setLevel(informationSource.getLevel()-1);
							informationSourceDao.modiflyInformationSource(informationSource);
						}
					}
				}
				if (informationSource.getId()==id) {
					if (count!=0) {
						informationSource.setLevel(count);
					}
					
					informationSource.setName(name);
					informationSource.setRemark(remark);
					informationSource.setSuperId(superId);
					informationSourceDao.modiflyInformationSource(informationSource);
				}
				
			}
			
			
			
			logger.info("修改信息渠道成功");
			return true;
		} catch (Exception e) {
			logger.error("修改信息渠道失败");
			return false;
		}
		
	}
	
	@Override
	public void modifyInfoClassification(InformationClassification iClassification) {
		// TODO Auto-generated method stub
		informationClassificationDao.modifyClassification(iClassification);
	}

	@Override
	public void delInfoSort(InformationSort iSort) {
		// TODO Auto-generated method stub
		informationSortDao.deleteISort(iSort);
	}

	@Override
	public void delInfoSource(InformationSource iSource) {
		// TODO Auto-generated method stub
		informationSourceDao.deleteInformationSourcer(iSource);
	}

	@Override
	public void delInfoClassification(InformationClassification iClassification) {
		// TODO Auto-generated method stub
		informationClassificationDao.deleteClassification(iClassification);
	}

	@Override
	public void modifyPowerModule(PowerModule pModule) {
		// TODO Auto-generated method stub
		powerModuleDao.modifly(pModule);
	}

	@Override
	public PowerModule getPowerModuleByName(String name) {
		// TODO Auto-generated method stub
		return powerModuleDao.getModuleByName(name);
	}

	@Override
	public List<PrisonRole> getAllPrisonRole() {
		// TODO Auto-generated method stub
		return prisonRoleDao.getAllPrisonRole();
	}

	@Override
	public Role addPrisonRole(Role role,PowerRole powerRole,String portStr) {
		try {
			role=roleDao.addRole(role);
			int roleId=role.getId();
			powerRole.setRoleId(roleId);
			
			if(portStr.equals("")){
				
			}else{
				List<InformationPort> ports=informationPortDao.getAllInfoPort();
				String[] portId=portStr.split(",");
				portStr=portStr+",";
				for (int i = 0; i < portId.length; i++) {
					portStr=portStr+getAllChildPort(Integer.parseInt(portId[i]),ports);
				}
				portId=portStr.split(",");
				for (int i = 0; i < portId.length; i++) {
					InformationPortPower informationPortPower =new InformationPortPower();
					informationPortPower.setRoleId(roleId);
					informationPortPower.setPortId(Integer.parseInt(portId[i]));
					informationPortPowerDao.addPortPower(informationPortPower);
				}
				
			}
			
			
			powerRoleDao.addPowerRole(powerRole);
			logger.info("添加角色成功");
			return role;
		} catch (Exception e) {
			logger.error("添加角色失败");
			return null;
		}
		
	}
	private String getAllChildPort(int  portId,List<InformationPort> ports){
		String childPort="";
		for(InformationPort port :ports){
			if (port.getSuperId()==portId) {
				childPort+=Integer.toString(port.getId())+",";
				childPort=childPort+ getAllChildPort(port.getId(),ports);
			}
			
		}
		return childPort;
	}
	@Override
	public boolean delPrisonRole(Role role) {
		try {
			roleDao.deleteRole(role);
			powerRoleDao.deletePowerRoleByRoleId(role.getId());
			informationPortPowerDao.deletePortPowerByRoleId(role.getId());
			logger.info("删除监狱角色成功！");
			return true;
		} catch (Exception e) {
			logger.error("删除监狱角色失败！！！");
			return false;
		}
		
	}
	@Override
	public boolean modifyPrisonRole(Role pRole,PowerRole powerRole,String portStr) {
		try {
			
			int roleId=pRole.getId();
			roleDao.modifly(pRole);
			powerRole.setRoleId(roleId);
			powerRoleDao.modifly(powerRole);
			informationPortPowerDao.deletePortPowerByRoleId(roleId);
			if (portStr.equals("")||portStr.equals(null)||portStr.equals("null")) {
				
			}else{
				List<InformationPort> ports=informationPortDao.getAllInfoPort();
				String[] idStrings=portStr.split(",");
				portStr=portStr+",";
				for (int i = 0; i < idStrings.length; i++) {
					portStr=portStr+getAllChildPort(Integer.parseInt(idStrings[i]),ports);
				}
				idStrings=portStr.split(",");
				for (int i = 0; i < idStrings.length; i++) {
					InformationPortPower portPower =new InformationPortPower();
					portPower.setPortId(Integer.parseInt(idStrings[i]));
					portPower.setRoleId(roleId);
					informationPortPowerDao.addPortPower(portPower);
				}
			}
			
			logger.info("修改监狱角色成功！");
			return true;
		} catch (Exception e) {
			
			logger.error("修改监狱结构失败！！！");
			return false;
		}
		
	}

	@Override
	public List<PrisonRole> getRoleByStructureId(PrisonStructure pStructure) {
		try {
			List<PrisonRole> pRoles=prisonRoleDao.getRoleByStructureId(pStructure);
			logger.info("通过部门id获取监狱角色成功！");
			return pRoles;
		} catch (Exception e) {
			logger.info("通过部门id获取监狱角色失败！！！");
			return null;
		}
		
	}

	@Override
	public List<PowerModule> getAllModule() {
		try {
			List<PowerModule> pModules=powerModuleDao.getAllModule();
			logger.info("获取所有权限模块成功！");
			return pModules;
		} catch (Exception e) {
			logger.error("获取所有权限模块失败！！！");
			return null;
		}
		
	}

	@Override
	public List<PowerRole> getPowerByRoleId(Integer roleId) {
		try {
			List<PowerRole> powerRoles=powerRoleDao.getPowerByRoleId(roleId);
			logger.info("通过角色id获取权限模块成功！");
			return  powerRoles;
		} catch (Exception e) {
			logger.error("通过角色id获取权限模块失败！！！");
			return null;
		}
		
	}

	@Override
	public Role getPrisonRoleById(Integer Id) {
		try {
			logger.info("通过id获取监狱角色成功！");
			return roleDao.getRole(Id);
		} catch (Exception e) {
			logger.error("通过id获取监狱角色失败！！！");
			return null;
		}
	}

	@Override
	public ConferenceRecord addConferenceRecord(ConferenceRecord conferenceRecord,Information information) {
		// TODO Auto-generated method stub
		try {
			String str="经与会人员讨论决定编号为"+information.getInformationAccount()+"的信息的措施是"+information.getMesure()+"，标准是"+information.getStandard()+"。";
			conferenceRecord.setConferenceContent(str);
			conferenceRecord.setInfoId(information.getId());
			conferenceRecord=conferenceRecordDao.addRecord(conferenceRecord);
			logger.info("添加会议记录成功！");
			return conferenceRecord;
		} catch (Exception e) {
			logger.error("添加会议记录失败！！！");
			return null;
		}
	}

	@Override
	public List<UserInfo> getUserListByAccountOrName(String userAccountOrName) {
		// TODO Auto-generated method stub
		try {
			
			List<UserInfo> uList=userInfoDao.getUserListByUserAccountOrName(userAccountOrName);
			if(uList.size()==0){
				return null;
			}
			return uList;
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
		
	}

	@Override
	public ConferenceRecord getRecordByInfoId(Integer infoId) {
		return conferenceRecordDao.getRecordByinfoId(infoId);
	}

	@Override
	public boolean modifyUserPassword(UserInfo userInfo) {
		try {
			userInfoDao.modifly(userInfo);
			return true;
		} catch (Exception e) {
			return false;
		}
		
	}
	@Override
	public UserView getUserViewById(Integer id) {
		UserView uView= userInfoDao.getUserViewByUserId(id);
		try{
			if(uView==null||uView.getUserId()==0){
				UserInfo user=userInfoDao.getUser(id);
				uView=new UserView(user.getUserId(),user.getUserName(),user.getUserAccount(),user.getUserHeadPortait(),user.getPhone(),user.getIdNumber(),user.getCreateUserid(),user.getCreateTimg(),user.isEnable());
				
			}
		}catch(Exception e){
			UserInfo user=userInfoDao.getUser(id);
			UserView u=new UserView(user.getUserId(),user.getUserName(),user.getUserAccount(),user.getUserHeadPortait(),user.getPhone(),user.getIdNumber(),user.getCreateUserid(),user.getCreateTimg(),user.isEnable());
							
			return u;			
		}
		try{
			Position position= userPositionDao.getPositionByUser(uView.getUserId());
			uView.setPositionName(position.getPositionName());
			uView.setPositionId(position.getId());
		}catch (Exception e) {			
			e.printStackTrace();
		}
		
		try{
			
			//Role role=roleDao.getRole();
			uView.setRoleId(powerRoleDao.getUserRolePower(uView.getUserId()).getRoleId());
			//uView.setRoleName(roleName);
			
		}catch (Exception e) {			
			e.printStackTrace();
		}
		try {		
			uView.setStructId(Integer.toString(userStructureDao.getStructureByUserId(uView.getUserId()).getStructureId()));
			uView.setStructName(userStructureDao.getStructureByUserId(uView.getUserId()).getStructureName());
			return uView;
		} catch (Exception e) {				
			return uView;
		}
		
	}

	@Override
	public PrisonStructure addSuperStructure(PrisonStructure pStructure) {
		try {
			pStructure.setSuperId(0);
			if(pStructure.getValue().equals("Department")){
				pStructure.setSuperId(1);
			}
			pStructure= prisonStructureDao.addStructure(pStructure);
			logger.info("添加监狱上级结构成功！");
			return pStructure;
		} catch (Exception e) {
			logger.error("添加监狱上级结构失败！！！");
			return null;
		}
		
		
	}

	@Override
	public List<PrisonStructure> getAllSuperStructure() {
		try {
			List<PrisonStructure> pStructures=	prisonStructureDao.getAllSuperStructure();
			logger.info("获取所有监狱结构成功！");
			return pStructures;
		} catch (Exception e) {
			logger.error("获取所有监狱结构失败！！！");
			return null;
		}
	}
	@Override
	public List<PrisonStructure> getAllPrisonArea() {
		try {
			List<PrisonStructure> pStructures=	prisonStructureDao.getAllPrisonArea();
			logger.info("获取所有监区成功！");
			return pStructures;
		} catch (Exception e) {
			logger.error("获取所有监区失败！！！");
			return null;
		}
	}


	@Override
	public boolean userUpdataInformation(ConferenceRecord conferenceRecord,Information information, Information oldInfo,UserInfo userInfo, String ip) {
		try{
			
			informationDao.modifyInformation(information);
			if(conferenceRecord!=null){
				addConferenceRecord(conferenceRecord, information);
			}
			InformationOperationRecord operationRecord= userDoRecordService.createUserDoRecord(information.getId(), userInfo.getUserId(), ip, ConfigStatic.DOTYPETABLE.get("InformationUpDate"), "修改信息", userInfo.getUserName());						
			addModifyInfoRecord( oldInfo, operationRecord.getId() );
//			userSubService.subToTaskDispatch(information.getId(), userInfo, ip);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	@Override
	public boolean infoResearchUpdataInformation(ConferenceRecord conferenceRecord,Information information, Information oldInfo,UserInfo userInfo, String ip,String flag) {
		try{
			informationDao.modifyInformation(information);
			if(conferenceRecord!=null){
				addConferenceRecord(conferenceRecord, information);
			}
			InformationOperationRecord operationRecord= userDoRecordService.createUserDoRecord(information.getId(), userInfo.getUserId(), ip, ConfigStatic.DOTYPETABLE.get("InformationUpDate"), "修改信息", userInfo.getUserName());						
			addModifyInfoRecord( oldInfo, operationRecord.getId() );
			if (flag.equals("0")) {
				userSubService.subToTaskDispatch(information.getId(), userInfo, ip);
			}
			
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	
	private void addModifyInfoRecord(Information information,int recordId){
		ModifyInformationRecord mRecord=new ModifyInformationRecord();
		mRecord.setRecordId(recordId);
		mRecord.setAppendix(information.getAppendix());
		mRecord.setCreateTime(information.getCreateTime());
		mRecord.setInformationAccount(information.getInformationAccount());
		mRecord.setInformationClassificationNames(information.getInformationClassificationNames());
		mRecord.setInformationConternt(information.getInformationConternt());
		mRecord.setInformationId(information.getId());
		mRecord.setInformationProfile(information.getInformationProfile());
		mRecord.setInformationSortNames(information.getInformationSortNames());
		
		mRecord.setInformationSourceNames(StringManageUtil.removeDot(information.getInformationSourceNames()));
		mRecord.setInformationType(information.getInformationType());
		mRecord.setInputPerSonNames(StringManageUtil.removeDot(information.getInputPerSonNames()));
		mRecord.setMesure(information.getMesure());
		mRecord.setSafetyPortNames(information.getSafetyPortNames());
		mRecord.setPrisonId(information.getPrisonId());
		mRecord.setStandard(information.getStandard());
		mRecord.setTaskCompletion(information.getTaskCompletion());
		mRecord.setTaskDispatchName(information.getTaskDispatchName());
		mRecord.setTaskDispatchTime(information.getTaskDispatchTime());
		mRecord.setUserId(information.getUserId());
		mRecord.setZeRenPerSonNames(information.getZeRenPerSonNames());
		mRecord.setZeRenRemark(information.getZeRenRemark());
		mRecord.setZeRenTime(information.getZeRenTime());
		modifyInformationRecordDao.addIRecord(mRecord);
	}


	@Override
	public List<InformationOperationRecord> getOperationByInfoId(Integer id) {
		try {
			List<InformationOperationRecord> operationRecords =informationOperationRecordDao.getOperationByInfoId(id);
			return operationRecords;
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public List<TaskDispatch> getTaskDispatchByInfoId(int infoId) {
		try {
			List<TaskDispatch> taskDispatchs =taskDispatchDao.getTaskDispatchByInfoId(infoId);
			return taskDispatchs;
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public CommentRecord getCommentRecordByInfoId(int infoId) {
		try {
			CommentRecord cRecord =commentRecordDao.getCommentRecordByInfoId(infoId);
			if(cRecord==null){
				return null;
			}else{
				return cRecord;
			}
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public EvaluationRecord getEvaluationRecordByInfoId(int infoId,Integer userId) {
		try {
			EvaluationRecord evaluationRecord =evaluationRecordDao.getEvaluationRecordByInfoId(infoId,userId);
			if(evaluationRecord==null){
				return null;
			}else{
				return evaluationRecord;
			}
		} catch (Exception e) {
			return null;
		}
	}
	@Override
	public List<EvaluationRecord>  getAllEvaluationRecord(int infoId,Integer userId) {
		try {
			List<EvaluationRecord>  eList =evaluationRecordDao.getAllEvaluationRecord(infoId);
			if(eList==null||eList.isEmpty()||eList.size()==0){
				return null;
			}else{
				return eList;
			}
		} catch (Exception e) {
			return null;
		}
	}
	@Override
	public boolean addCommentRecordBrowse(int infoId,UserInfo u) {
		try {
			
			CommentRecordBrowse commentRecordBrowse = new CommentRecordBrowse(infoId, u.getUserId(), u.getUserName(), System.currentTimeMillis());
			commentRecordBrowseDao.addBrowse(commentRecordBrowse);
			return true;
		} catch (Exception e) {
			
			return false;
		}
	}
	@Override
	public boolean addEvaluationRecordBrowse(int infoId,UserInfo u) {
		try {
			
			EvaluationRecordBrowse evaluationRecord = new EvaluationRecordBrowse(infoId, u.getUserId(), u.getUserName(), System.currentTimeMillis());
			evaluationRecordBrowseDao.addRBrowse(evaluationRecord);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	@Override
	public boolean modifyEvaluationRecordBrowse(int infoId,UserInfo u) {
		try {
			
			evaluationRecordBrowseDao.bulkUpdate(infoId, u.getUserId());
//			evaluationRecordBrowse.setLook(true);
//			evaluationRecordBrowse.setCreateTime(System.currentTimeMillis());
//			evaluationRecordBrowseDao.modifly(evaluationRecordBrowse);
			return true;
		} catch (Exception e) {
			
			return false;
		}
	}
	@Override
	public boolean modifyCommentRecordBrowse(int infoId,int id,UserInfo u) {
		try {
			
			CommentRecordBrowse commentRecordBrowse = new CommentRecordBrowse(infoId, u.getUserId(), u.getUserName(), System.currentTimeMillis());
			commentRecordBrowse.setId(id);
			commentRecordBrowseDao.modifly(commentRecordBrowse);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	@Override
	public List<CommentRecordBrowse> getCommentRecordBrowseByInfoId(int infoId) {
		try {
			List<CommentRecordBrowse> cList = commentRecordBrowseDao.getCommentRecordBrowseByInfoId(infoId);
			return cList;
		} catch (Exception e) {
			return null;
		}
	}
	@Override
	public List<EvaluationRecordBrowse> getEvaluationRecordBrowseByInfoId(int infoId,int eRecordId) {
		try {
			List<EvaluationRecordBrowse> cList = evaluationRecordBrowseDao.getEvaluationRecordBrowseByInfoId(infoId,eRecordId);
			return cList;
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public List<InformationPort> getAllInformationPort() {
		try {
			List<InformationPort> iPorts = informationPortDao.getAllInfoPort();
			logger.info("获取所有信息分口成功！");
			return iPorts;
 		} catch (Exception e) {
 			logger.error("获取所有信息分口失败！！！");
			return null;
		}
	}

	@Override
	public boolean modifyInformationPort(Integer id,Integer level,Integer superId, String name,String remark,Integer lastSuperId) {
//		try {
//			informationPortDao.modifyInfoPort(informationPort);
//			logger.info("修改信息分口成功");
//			return true;
//		} catch (Exception e) {
//			logger.error("修改信息分口失败！！！");
//			return false;
//		}
		try {
			List<InformationPort> ports = informationPortDao.getAllInfoPort();
			int count=0;
			for (InformationPort informationPort : ports) {
				if (superId!=lastSuperId) {
					if (informationPort.getId()==superId) {
						count++;
					}
					if (informationPort.getId()==lastSuperId) {
						if (informationPort.getLevel()>level) {
							informationPort.setLevel(informationPort.getLevel()-1);
							informationPortDao.modifyInfoPort(informationPort);
						}
					}
				}
				if (informationPort.getId()==id) {
					if (count!=0) {
						informationPort.setLevel(count);
					}
					informationPort.setPortName(name);
					informationPort.setRemark(remark);
					informationPort.setSuperId(superId);
					informationPortDao.modifyInfoPort(informationPort);
				}
				
			}
			
			
			
			logger.info("修改信息分口成功");
			return true;
		} catch (Exception e) {
			logger.error("修改信息分口失败");
			return false;
		}
	}

	@Override
	public InformationPort addInformationPort(InformationPort informationPort) {
		try {
			
			int count=informationPortDao.getCountPort(informationPort.getSuperId());
			informationPort.setLevel(count+1);
			informationPort = informationPortDao.addInfoPort(informationPort);
			return informationPort;
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public boolean deleteInformationPort(InformationPort informationPort) {
		try {
			informationPortDao.deleteInfoPort(informationPort);
			logger.info("删除信息分口成功");
			return true;
		} catch (Exception e) {
			logger.error("删除信息分口失败！！！");
			return false;
		}
	}

	@Override
	public List<InformationPortPower> getPortPowerByRoleId(Integer roleId) {
		try {
			List<InformationPortPower> portPowers =informationPortPowerDao.getPortPowerByRoleId(roleId);
			return portPowers;
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public boolean addPosition(Position position) {
		try {
			positionDao.addPosition(position);
			logger.info("添加职务成功");
			return true;
		} catch (Exception e) {
			logger.error("添加职务失败");
			return false;
		}
	}

	@Override
	public boolean addUserPosition(UserPosition userPosition) {
		try {
			userPositionDao.addUserPosition(userPosition);
			
			logger.info("添加用户职务成功");
			return true;
		} catch (Exception e) {
			logger.error("添加用户职务失败");
			return false;
		}
	}

	@Override
	public boolean deletePosition(Position position) {
		try {
			positionDao.deletePosition(position);
			logger.info("删除职务成功");
			return true;
		} catch (Exception e) {
			logger.error("删除职务失败");
			return false;
		}
		
	}

	@Override
	public boolean deleteUserPosition(UserPosition userPosition) {
		try {
			userPositionDao.deleteUserPosition(userPosition);
			logger.info("删除用户职务成功");
			return true;
		} catch (Exception e) {
			logger.error("删除用户职务失败");
			return false;
		}
		
	}

	@Override
	public boolean modifyPosition(Position position) {
		try {
			positionDao.modifly(position);
			logger.info("修改职务成功");
			return true;
		} catch (Exception e) {
			logger.error("修改职务失败");
			return false;
		}
		
	}

	@Override
	public boolean modifyUserPosition(UserPosition userPosition) {
		// TODO Auto-generated method stub
		try {
			userPositionDao.modifly(userPosition);
			logger.info("修改用户职务成功");
			return true;
		} catch (Exception e) {
			logger.error("修改用户职务失败");
			return false;
		}
	}

	@Override
	public List<Position> getAllPsoition( ) {
		try {
			 List<Position> positions= positionDao.getAllPosition();
			 logger.info("获取职务成功");
			return positions;
		} catch (Exception e) {
			logger.error("获取职务失败");
			return null;
		}
	}

	@Override
	public List<UserPosition> getUserPositionByUserId(Integer userId) {
		try {
			List<UserPosition> userPositions = userPositionDao.getPositionByUserId(userId);
			logger.info("获取用户职务成功");
			return userPositions;
		} catch (Exception e) {
			logger.error("获取用户职务失败");
			return null;
		}
	}

	@Override
	public boolean addRole(Role role) {
		try {
			roleDao.addRole(role);
			logger.info("添加角色成功");
			return true;
		} catch (Exception e) {
			logger.error("添加角色失败");
			return false;
		}
	}

	@Override
	public boolean deleteRole(Role role) {
		try {
			roleDao.deleteRole(role);
			logger.info("删除角色成功");
			return true;
		} catch (Exception e) {
			logger.error("删除角色失败");
			return false;
		}
	}

	@Override
	public boolean modifyRole(Role role) {
		try {
			roleDao.modifly(role);
			logger.info("修改角色成功");
			return true;
		} catch (Exception e) {
			logger.error("修改角色失败");
			return false;
		}
	}

	@Override
	public List<Role> getAllRole() {
		try {
			List<Role> roles=roleDao.getAllRole();
			logger.info("获取所有角色成功");
			return roles;
		} catch (Exception e) {
			logger.error("获取所有角色失败");
			return null;
		}
	}

	@Override
	public Page<UserView> getUserList(UserQuery userQuery,Integer structureId, Page<UserView> p) {
		Page<UserView> rsP=userInfoDao.getStruectureAllUser(userQuery,structureId , p);
		List<UserView> uList=rsP.getList();			
		List<UserView> rsList=new ArrayList<UserView>();		
		for(int i=0;i<uList.size();i++){
			UserView curU=(UserView)uList.get(i);
//			Position position = positionDao.getPosition(curU.getPositionId());
//			curU.setPositionName(position.getPositionName());	
//			curU.setStructId(Integer.toString(userStructureDao.getStructureByUserId(curU.getUserId()).getStructureId()));
//			curU.setStructName(userStructureDao.getStructureByUserId(curU.getUserId()).getStructureName());			
			rsList.add(curU);
		}	
		rsP.setList(rsList);
		return rsP;
	}

	@Override
	public List<PrisonStructure> getDepartmentAndPrisonArea() {
		try {
			List<PrisonStructure> pStructures=	prisonStructureDao.getDepartmentAndPrisonArea();
			logger.info("获取所有科室和监区成功！");
			return pStructures;
		} catch (Exception e) {
			logger.error("获取所有科室和监区失败！！！");
			return null;
		}
	}
	@Override
	public Position getPosition(Integer id) {
		try {
			Position position = positionDao.getPosition(id);
			logger.info("获取职务信息成功");
			return position;
			
		} catch (Exception e) {
			logger.info("获取职务信息失败");
			return null;
		}
	}
	@Override
	public boolean infoSoureCompositor(String idStr, String levelStr) {
		try {
			String[] id=idStr.split(",");
			List<InformationSource> sources =informationSourceDao.getAllInformationSource();
			String level[]=levelStr.split(",");
			for (InformationSource informationSource : sources) {
				for (int i = 0; i < id.length; i++) {
					if (informationSource.getId()==Integer.parseInt(id[i])) {
						informationSource.setLevel(Integer.parseInt(level[i]));
						informationSourceDao.modiflyInformationSource(informationSource);
					}
					
					
				}
			}
			
			
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
	}
	@Override
	public List<InformationSource> getInformationSourceBySuperId(Integer superId) {
		return informationSourceDao.getSourceBysuperId(superId);
	}
	@Override
	public List<InformationPort> getInformationPortBySuperId(Integer superId) {
		
		return informationPortDao.getPortBysuperId(superId);
	}
	@Override
	public boolean infoPortCompositor(String idStr, String levelStr) {
		try {
			String[] id=idStr.split(",");
			String level[]=levelStr.split(",");
			List<InformationPort> ports =informationPortDao.getAllInfoPort();
			for (InformationPort informationPort : ports) {
				for (int i = 0; i < id.length; i++) {
					if (informationPort.getId()==Integer.parseInt(id[i])) {
						informationPort.setLevel(Integer.parseInt(level[i]));
						informationPortDao.modifyInfoPort(informationPort);
					}
				}
			}
			
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	@Override
	public InformationPort getInformationPortById(Integer id) {
		try {
			InformationPort port = informationPortDao.getInfoPort(id);
			logger.info("根据id获取分口成功！");
			return port;
		} catch (Exception e) {
			logger.error("根据id获取分口失败！");
			return null;
		}
	}
	@Override
	public InformationSource getInformationSourceById(Integer id) {
		try {
			InformationSource source = informationSourceDao.getInformationSource(id);
			logger.info("根据id获取渠道成功！");
			return source;
		} catch (Exception e) {
			logger.error("根据id获取渠道失败！");
			return null;
		}
	}
	@Override
	public ModifyInformationRecord getModifyRecordByInfoIdAndRecoredId(Integer recordId, Integer infoId) {
		try {
			ModifyInformationRecord mRecord = modifyInformationRecordDao.getModifyRecordByInfoIdAndRecoredId(recordId, infoId);
			return mRecord;
		} catch (Exception e) {
			return null;
		}
	}
	@Override
	public List<PrisonStructure> getAllQuestionStructure() {
	try {
		List<PrisonStructure> prisonStructures=prisonStructureDao.getAllQuestionStructure();
		logger.info("获取类型为空的部门成功！");
		return prisonStructures;
	} catch (Exception e) {
		logger.error("获取类型为空的部门失败！");
		return null;
	}
	}
	
	@Override
	public boolean modifyQuestionStructure(PrisonStructure pStructure) {
	try {
		int num=pStructure.getNum();
		String typeName=pStructure.getTypeName();
		String value=pStructure.getValue();
		pStructure=prisonStructureDao.getStructure(pStructure.getId());
		pStructure.setNum(num);
		pStructure.setTypeName(typeName);
		pStructure.setValue(value);
		prisonStructureDao.modifly(pStructure);
		logger.info("修改类型为空的部门成功！");
		return true;
	} catch (Exception e) {
		logger.error("修改类型为空的部门失败！");
		return false;
	}
	}

}
